function link_Number(input) {
    const valueBlock = document.createElement('value');
    valueBlock.setAttribute('name', input);
    const numberBlock = document.createElement('shadow');
    numberBlock.setAttribute('type','math_number');
    const newNumber = document.createElement('field');
    newNumber.setAttribute('name', 'NUM');
    newNumber.textContent = '0';
    numberBlock.appendChild(newNumber);
    valueBlock.appendChild(numberBlock);
    return valueBlock;
}


function addvoice() {
    // 创建新的类别元素
    const newCategory = document.createElement('category');
    newCategory.setAttribute('name', '智慧语音');
    const Category = ["voice_iat","voice_collect","voice_tts","voice_aiui"]
    // 创建新的块元素并添加到新类别中
    for (let i = 0; i<Category.length; i++){
        const newBlock = document.createElement('block');
        newBlock.setAttribute('type', Category[i]);
        newCategory.appendChild(newBlock);
        // 将新类别添加到工具箱中
        toolbox.appendChild(newCategory);
    }
    workspace.updateToolbox(toolbox);
}


function addarm() {
    // 创建新的类别元素
    const newCategory = document.createElement('category');
    newCategory.setAttribute('name', '机械臂控制');
    const Category = ["get_markposition","set_pump","get_marklist"]
    // 创建新的块元素并添加到新类别中
    for (let i = 0; i<Category.length; i++){
        var newBlock = document.createElement('block');
        newBlock.setAttribute('type', Category[i]);
        newCategory.appendChild(newBlock);
        // 将新类别添加到工具箱中
        toolbox.appendChild(newCategory);
    }
    newBlock = document.createElement('block');
    newBlock.setAttribute('type', 'set_endservos');
    newBlock.appendChild(link_Number('NAME'))
    newCategory.appendChild(newBlock);
    // 将新类别添加到工具箱中
    toolbox.appendChild(newCategory);
    newBlock = document.createElement('block');
    newBlock.setAttribute('type', 'goto_position');
    newBlock.appendChild(link_Number('x'))
    newBlock.appendChild(link_Number('y'))
    newBlock.appendChild(link_Number('z'))
    newCategory.appendChild(newBlock);
    // 将新类别添加到工具箱中
    toolbox.appendChild(newCategory);

    workspace.updateToolbox(toolbox);
}
  

const intervalId = setInterval(function () {
    rei_log('获取机器人信息中')
    // console.log(robotData)
    if (typeof robotData['robot_ID'] != 'undefined'){
        try{
            rei_robot = new rei_robot_core({robotName:robotData['robot_ID']})
        }catch(error){
            console.log('未能识别到设备')
        }
        //增加新的设备
        // rei_robot.addRoboCard({
        //     CardID:'bobac3-tag-button',
        //     ImgSource:'static/img/bobac3.png',
        //     CardName:'Bobac',
        //     CardTool:'bobac-tool'
        // })
        // rei_robot.addRoboCard({
        //     CardID:'swift-pro-button',
        //     ImgSource:'static/img/bobac3.png',
        //     CardName:'swift-pro',
        //     CardTool:'uarm-swift-pro'
        // })
        rei_log('识别到设备:'+robotData['robot_ID'])
        clearInterval(intervalId)

        var selectElement = document.getElementById('maplist')
        mapList.forEach(map=>{
            var newoption = new Option(map,map)
            selectElement.appendChild(newoption)
        })
        updata_Goallist()
        if(robotData['robot_ID'].indexOf('bobac')>=0){
            addvoice()
            // addarm()
        }
        // 加载界面
        $(".loader-wrapper").fadeOut("slow")

    }else{
        rei_log('未查询到机器人信息')
        inter++
        rei_log('一秒后进行第'+inter+'次尝试')
    }

}, 1000); // 每隔2秒执行一次

